/*
 * CS_REQ_CHANNEL_LIST.cpp
 *
 *  Created on: 2013. 11. 5.
 *      Author: the2
 */



#include "../../Command.h"

#include "../../User.h"
#include "../../Config.h"
#include "../../NLogic/Static.h"


namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_CHANNEL_LIST(User * pUser, ::Network::Packet * pPacket)
{
	/*
	/// 채널리스트 요청 C -> S
	CS_REQ_CHANNEL_LIST			0x0911
	{
		u16 u16server_id;
	}

	SC_REQ_CHANNEL_LIST			0x0912
	{
		s16 s16ack;
		u16 u16server_id;	//
		str hostAddress;		// u32	u32ip;			// channel ip
		u16	u16port;			// channel port
		u16	u16maxUser;	// channel 에 참가할 수 있는 최대 인원

		u16 	u16count
		{
			u16	u16chID;		// channel ID
			u16 	u16user;  	// channel 에 참여하고 있는 인원
		}
	}
	 */

	PREPARE_PACKET;

	uint16_t server_id = 0;
	* pPacket >> server_id;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

	::ProjectZ::NLogic::ServerAddrCCU * clsSvrAddr = ::ProjectZ::NLogic::Static::Instance().GETSERVERADDRCCU();
	if(NULL == clsSvrAddr)
	{
		* pAck << static_cast<int16_t>(ACK_NOT_FOUND_SERVER) << static_cast<uint16_t>(0);
		pAck->MakePacket(CMD_SC_REQ_CHANNEL_LIST);
		pUser->Send(pAck);
		return;
	}

	DEBUGLOG("serverid = %d", server_id);
	auto iter = clsSvrAddr->GetServerAddrTree().find(server_id);
	if(iter == clsSvrAddr->GetServerAddrTree().end())
	{
		DEBUGLOG("serverid = %d not found", server_id);
		* pAck << static_cast<int16_t>(ACK_NOT_FOUND_SERVER) << static_cast<uint16_t>(server_id)
				<< static_cast<uint16_t>(0) << static_cast<uint16_t>(0)
				<< static_cast<uint16_t>(0) << static_cast<uint16_t>(0);
		pAck->MakePacket(CMD_SC_REQ_CHANNEL_LIST);
		pUser->Send(pAck);
		return;
	}

	* pAck << static_cast<int16_t>(ACK_OK) << static_cast<uint16_t>(server_id)
			<< iter->second._domainName.c_str()
			<< static_cast<uint16_t>(iter->second._port)
			<< static_cast<uint16_t>(Config::InstancePtr()->GetResourceDatabase()._maxChannelUser);

	* pAck	<< static_cast<uint16_t>(NLogic::ServerAddrCCU::__MAX__ - 1);

	for(int i=NLogic::ServerAddrCCU::__NONE__ + 1; i < NLogic::ServerAddrCCU::__MAX__; i++)
	{
		* pAck << static_cast<uint16_t>(i)
				<< static_cast<uint16_t>(iter->second.GetChannelData(i));
	}

	pAck->MakePacket(CMD_SC_REQ_CHANNEL_LIST);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/

